package com.koron.css2.serviceManage.impl;

import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.TWaterCompanyBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.TWaterCompanyMapper;
import com.koron.css2.serviceManage.utils.CommonUtils;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.mysql.cj.util.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

public class CompanyUpdate implements ServerInterface {

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {

		@SuppressWarnings("rawtypes")
		MessageBean<PageInfo> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", PageInfo.class);

		try {
			TWaterCompanyBean bean = JsonUtils.objectToPojo(req.getData(), TWaterCompanyBean.class);
			if(StringUtils.isNullOrEmpty(bean.getCompanyId())){
				info.setCode(Constant.MESSAGE_INT_FAIL);
				info.setDescription("CompanyId不能为空");
				return info;
			}
			TWaterCompanyMapper mapper = factory.getMapper(TWaterCompanyMapper.class);
			//校验字段重复
			if(mapper.check2("COMPANY_NO", bean.getCompanyNo(), bean.getCompanyId())>0) {
				info.setCode(Constant.MESSAGE_INT_FAIL);
				info.setDescription("水司编号为："+bean.getCompanyNo()+"的信息已存在。");
				return info;
			}
			if(mapper.check2("SIMPLIFY_NO", bean.getSimplifyNo(), bean.getCompanyId())>0) {
				info.setCode(Constant.MESSAGE_INT_FAIL);
				info.setDescription("简化编号为："+bean.getSimplifyNo()+"的信息已存在。");
				return info;
			}
			if(mapper.check2("SHORT_NAME", bean.getShortName(), bean.getCompanyId())>0) {
				info.setCode(Constant.MESSAGE_INT_FAIL);
				info.setDescription("水司简称为："+bean.getCompanyNo()+"的信息已存在。");
				return info;
			}
//			if(mapper.check2("OA_CODE", bean.getOaCode(), bean.getCompanyId())>0) {
//				info.setCode(Constant.MESSAGE_INT_FAIL);
//				info.setDescription("水司关联的OA："+bean.getCompanyNo()+"的信息已存在。");
//				return info;
//			}
			bean.setUpdateName(userInfo.getUserInfo().getName());
			bean.setUpdateTime(CommonUtils.getCurrentTime());
			mapper.update(bean);
			//TODO 创建数据库相关操作
		} catch (Exception e) {
			info.setCode(Constant.MESSAGE_INT_FAIL);
			info.setDescription(e.getMessage());
			logger.error(e.getMessage(), e);
		}
		return info;
	}

}
